/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eng.scc.core.editors;

import com.eng.scc.core.data.Address;
import com.eng.scc.core.data.Company;
import com.eng.scc.core.db.manager.service.FuelControllerManagerService;
import com.eng.scc.core.panes.AddressPane;
import com.eng.scc.core.util.Error;
import com.eng.scc.core.view.ApplicationContext;
import java.awt.Component;
import java.awt.Dimension;
import com.eng.scc.core.parts.FormattedTextField;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author Rafael
 */
public class CompanyEditor extends AbstractPanelEditor<Company>
{   
//    public static void main( String[] args )
//    {
//        CompanyEditor.edit( null, new Company() );
//    }
    
    /**
     * Creates new form CompanyEditor
     */
    public CompanyEditor()
    {
        initComponents();
    }

    public static boolean edit( Component parent, Company c, Address a ) throws Exception
    {
        CompanyEditor ce = new CompanyEditor();

        ce.setSource( c );
        ce.setAddress( a );

        boolean acp = DefaultEditor.createEditor( parent, 
                                           "Editor de Empresa", 
                                           "Formulário de preenchimento de dados da Empresa", 
                                           new Dimension( 550, 500 ), ce );
        
        if ( acp )
        {
            a = ce.getAddress();
            
            if ( a.getId() == 0 )
            {
                FuelControllerManagerService.getInstance().addAddress( a );
            }
            
            else
            {
                FuelControllerManagerService.getInstance().updateAddress( a );
            }
            
            c.setAddressId( a.getId() );
            
            if ( c.getId() == 0 )
            {
                FuelControllerManagerService.getInstance().addCompany(c );
            }
            
            else
            {
                FuelControllerManagerService.getInstance().updateCompany(c );
            }
        }
        
        
        return acp;
    }

    @Override
    public void validateEditor( Error error )
    {
        if ( tfRealName.getText() == null || tfRealName.getText().isEmpty() )
        {
            error.add( "Informe a Razão social!" );
        }
        
        if ( tfFantasyName.getText() == null || tfFantasyName.getText().isEmpty() )
        {
            error.add( "Informe o Nome Fantasia!" );
        }
        
        addressPane.validateForm( error );
    }
    
    @Override
    public void setSource( Company source )
    {
        if ( source == null )
        {
            throw new IllegalArgumentException( "Client cannot be null!" );
        }

        this.source = source;
        
        populateFields();
    }

    
    private void populateFields()
    {
        try
        {
//            addressPane.setAddress( new Address() );
//            
//            if ( source.getId() > 0 )
//            {
//                addressPane.setAddress( FuelControllerManagerService.getInstance().getAddress( source.getAddressId() ) );
//            }
            
            tfRealName.setText( source.getRealName() );
            tfFantasyName.setText( source.getFantasyName() );
            tfCNPJ.setText( source.getCnpj() );
            tfPhone1.setText( source.getPhone1() );
            tfPhone2.setText( source.getPhone2() );
            tfEmail.setText( source.getEmail() );
        }
        
        catch ( Exception e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
    }
    
    @Override
    public void getSource()
    {
        try
        {
            if ( source != null )
            {
//                this.source.setAddressId( addressPane.getAddress().getId() );
                this.source.setRealName( tfRealName.getText() );
                this.source.setFantasyName( tfFantasyName.getText() );
                this.source.setCnpj( tfCNPJ.getText() );
                this.source.setPhone1( tfPhone1.getText() );
                this.source.setPhone2( tfPhone2.getText() );
                this.source.setEmail( tfEmail.getText() );
            }
        }
        
        catch ( Exception e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
    }
    
    private void initComponents()
    {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel1 = new javax.swing.JLabel();
        tfRealName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        tfFantasyName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tfCNPJ = new FormattedTextField();
        addressPane = new com.eng.scc.core.panes.AddressPane();
        jLabel4 = new javax.swing.JLabel();
        tfEmail = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        tfPhone1 = new FormattedTextField();
        tfPhone2 = new FormattedTextField();

        try
        {
            tfPhone1.setFormatterFactory( new DefaultFormatterFactory( new MaskFormatter( "(##) #########" ) ) );
            tfPhone2.setFormatterFactory( new DefaultFormatterFactory( new MaskFormatter( "(##) #########" ) ) );
            tfCNPJ.setFormatterFactory( new DefaultFormatterFactory( new MaskFormatter( "##.###.###/####-##" ) ) );
        }
        catch ( Exception e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
        
        setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("Razão Social:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jLabel1, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(tfRealName, gridBagConstraints);

        jLabel2.setText("Nome Fantasia:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jLabel2, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(tfFantasyName, gridBagConstraints);

        jLabel3.setText("CNPJ:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        add(jLabel3, gridBagConstraints);

        tfCNPJ.setMinimumSize(new java.awt.Dimension(120, 20));
        tfCNPJ.setPreferredSize(new java.awt.Dimension(120, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(tfCNPJ, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(addressPane, gridBagConstraints);

        jLabel4.setText("Email:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jLabel4, gridBagConstraints);

        tfEmail.setMaximumSize(new java.awt.Dimension(90, 90));
        tfEmail.setPreferredSize(new java.awt.Dimension(120, 20));
        tfEmail.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(tfEmail, gridBagConstraints);

        jLabel5.setText("Telefone 1:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jLabel5, gridBagConstraints);

        jLabel6.setText("Telefone 2:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jLabel6, gridBagConstraints);
        
        tfPhone1.setMinimumSize(new java.awt.Dimension(120, 20));
        tfPhone1.setPreferredSize(new java.awt.Dimension(120, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(tfPhone1, gridBagConstraints);

        tfPhone2.setMinimumSize(new java.awt.Dimension(120, 20));
        tfPhone2.setPreferredSize(new java.awt.Dimension(120, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(tfPhone2, gridBagConstraints);
    }

    private com.eng.scc.core.panes.AddressPane addressPane;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private com.eng.scc.core.parts.FormattedTextField tfCNPJ;
    private javax.swing.JTextField tfEmail;
    private javax.swing.JTextField tfFantasyName;
    private com.eng.scc.core.parts.FormattedTextField tfPhone1;
    private com.eng.scc.core.parts.FormattedTextField tfPhone2;
    private javax.swing.JTextField tfRealName;
    // End of variables declaration//GEN-END:variables

    private void setAddress(Address a) {
        addressPane.setAddress(a);
    }

    public Address getAddress() {
        return addressPane.getAddress();
    }
    
    
}
